package com.mallcloud.mall.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.common.core.util.R;
import com.mallcloud.mall.product.api.entity.AttrValueModel;
import com.mallcloud.mall.product.service.AttrValueService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "属性值信息管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/product/attrValue")
public class AttrValueController {
    private final AttrValueService attrValueService;

    @ApiOperation("分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('product_attr_view')")
    public R<IPage<AttrValueModel>> page(Page page, AttrValueModel dto) {
        return R.ok(attrValueService.page(page, Wrappers.query(dto)));
    }

    @ApiOperation("列表查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('product_attr_view')")
    public R<List<AttrValueModel>> list(AttrValueModel dto) {
        return R.ok(attrValueService.list(Wrappers.query(dto)));
    }

    @ApiOperation("通过id查询")
    @GetMapping("/getInfo")
    @PreAuthorize("@pms.hasPermission('product_attr_view')")
    public R<AttrValueModel> getInfo(AttrValueModel dto) {
        return R.ok(attrValueService.getById(dto.getAttrValueId()));
    }

    @ApiOperation("新增")
    @PostMapping("/save")
    @PreAuthorize("@pms.hasPermission('product_attr_add')")
    public R<Boolean> save(@RequestBody AttrValueModel dto) {
        return R.ok(attrValueService.save(dto));
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @PreAuthorize("@pms.hasPermission('product_attr_edit')")
    public R<Boolean> updateById(@RequestBody AttrValueModel dto) {
        return R.ok(attrValueService.updateById(dto));
    }

    @ApiOperation("删除")
    @DeleteMapping("/{attrValueId}")
    @PreAuthorize("@pms.hasPermission('product_attr_del')")
    public R<Boolean> removeById(@PathVariable Integer attrValueId) {
        return R.ok(attrValueService.removeById(attrValueId));
    }

    @ApiOperation("批量删除属性值信息")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('product_attr_del')")
    public R<Boolean> removeByIds(@RequestBody List<Long> attrValueIds) {
        return R.ok(attrValueService.removeByIds(attrValueIds));
    }
}
